#include <Keyboard.h>

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO:       A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO:   2(SDA),  3(SCL), ...
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

int buttom_ = 4;
bool buttom_released = true;

bool run_once = false;
String password[] = {"a123456", "b123456", "c123456", "d123456", "e123456", "f123456", "g123456", "h123456"};
String ch_[] = {"taobao", "zhifu", "jd", "xigua", "bili", "bank", "tel-131", "game"};
int  point_ = 0;
unsigned long last_time = 0;
void setup() {
  Keyboard.begin();
  pinMode(buttom_ , INPUT_PULLUP);
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }

  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  delay(200); // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();



  // don't need to set anything up to use DigiKeyboard

}


void show_oled(int no_) {
  display.clearDisplay();

  display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
  display.println(F("Which ch:"));

display.println(ch_[point_]);

  display.display();

}


void loop() {
  // this is generally not necessary but with some older systems it seems to
  // prevent missing the first character after a delay:
  if (digitalRead(buttom_) == 0 and  buttom_released == true) {
    buttom_released = false;
    point_++;
    if (point_ >= 8) {
      point_ = 0;
    }
    //Keyboard.write(65);
    last_time = millis();
     show_oled(point_);
  }
  else if (digitalRead(buttom_) == 1 and  buttom_released == false) {
    buttom_released = true;
    run_once = true;
  }
  if (run_once == true) {
    if  ((millis() - last_time ) >= 3000) {

      // Type out this string letter by letter on the computer (assumes US-style
      // keyboard)
      Keyboard.print(password[point_]);
      run_once = false;

    }
  }
}